<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import AssistDevicePlanApi from '/@/api/webPath/AssistDevicePlanApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
defineOptions({
  name: 'AssistDevicePlanEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);

// 创建一个 Date 对象，表示当前时间
const now = new Date();

// 获取年份
const year = now.getFullYear(); // 获取完整的年份（例如：2023）
// 获取月份
const month = now.getMonth() + 1; // 获取月份（0-11，0 表示 1 月，所以需要 + 1）
// 获取日期
const day = now.getDate(); // 获取日期（1-31）
// 获取小时
const hours = now.getHours(); // 获取小时（0-23）
// 获取分钟
const minutes = now.getMinutes(); // 获取分钟（0-59）
// 获取秒数
const seconds = now.getSeconds(); // 获取秒数（0-59）
// 格式化年月日
const yearMonthDay = `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`;
// 格式化时分秒
const hoursMinutesSeconds = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;


const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'right',
  labelWidth: '120px',
  modelData: {},  //表单参数值 初始化
  callback: async (any: object) => {  //表单执行成功后回调
    //完成数据提交
    const { message } = await AssistDevicePlanApi.save(any)
    $baseMessage(message, 'success', 'vab-hey-message-success');
    dialogFormVisible.value = false


    //事件及数据推送到上一层父类
    emit('callBack', any); //把表单数据推送到上一层
  },
  items: [  //表单item数据初始化
    {
      label: "设备类型",
      field: "deviceType",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "维护周期",
      field: "cycle",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "维护内容",
      field: "content",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "维护日期",
      field: "planDate",
      clearable: true,
      type: FORM_FIELD_TYPE.DATE,
      defaultValue: yearMonthDay,
      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    {
      label: "责任部门/人员",
      field: "responsible",
      clearable: true,
      type: FORM_FIELD_TYPE.INPUT,

      placeholder: "请输入",

      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    },
    //   {
    //     label: "所属公司",
    //     field: "companyId",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "创建人",
    //     field: "creator",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "修改人",
    //     field: "reviser",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUT,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },
    //   {
    //     label: "排序",
    //     field: "sort",
    //     clearable: true,
    //     type: FORM_FIELD_TYPE.INPUTNUMBER,
    //     defaultValue: 0,

    //    placeholder:"请输入",

    //    rule:{
    //      trigger: 'blur',
    //      validator: (rule: any, value: any, callback: any) => {  //绑定验证
    //            callback();
    //        },
    //      },

    // },

  ],
})






//页面数据加载
onMounted(async () => {

  //初始化数据model
  if (props.modelValue.query) { //跳转过来时候有参数
    if (props.modelValue.query.assistDevicePlanId) {  //id存在则为编辑，加载网络数据，如上级页面带参数手动覆盖

      const { data } = await AssistDevicePlanApi.getDetail({
        assistDevicePlanId: props.modelValue.query.assistDevicePlanId   //从路由中获取参数
      })
      formDataModel.value.modelData = data;
    } else {
      //新增时候则用父类页面传参默认填充可根据业务修改
      for (const key in props.modelValue.query) {
        formDataModel.value.modelData[key] = props.modelValue.query[key]
      }
    }
  }
})


//方法暴露区域  todo
defineExpose({

})

</script>
